package gossip.v1;

import java.net.InetSocketAddress;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

public class MemberState {
    private final String nodeId;
    private final InetSocketAddress address;
    private final AtomicLong heartbeat = new AtomicLong(1);
    private volatile long updateTime = System.currentTimeMillis();
    private Map<String, String> metadata = new ConcurrentHashMap<>();

    public MemberState(String nodeId, InetSocketAddress address) {
        this.nodeId = nodeId;
        this.address = address;
    }

    public void incrementHeartbeat() {
        heartbeat.incrementAndGet();
        updateTime = System.currentTimeMillis();
    }

    public void updateTimestamp() {
        updateTime = System.currentTimeMillis();
    }

    public String getNodeId() {
        return nodeId;
    }

    public InetSocketAddress getAddress() {
        return address;
    }

    public long getHeartbeat() {
        return heartbeat.get();
    }

    public void setHeartbeat(long heartbeat) {
        this.heartbeat.set(heartbeat);
    }

    public long getUpdateTime() {
        return updateTime;
    }

    public Map<String, String> getMetadata() {
        return metadata;
    }

    public void setMetadata(Map<String, String> metadata) {
        this.metadata = metadata;
    }
}
